import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
} from "vue-router";
import type { RouteRecordRaw } from "vue-router";
const Layout = () => import("@/Layout/Layout.vue");
const LayoutView = () => import("@/Layout/LayoutView.vue");


// 自定义路由用这个
// const modules: AppRouteRecordRaw[] = []; // 全部引入路由列表
// const noModulesName:string[] = []; // 不准备引入的文件
// const files = import.meta.glob("./modules/*.ts",{eager:true}) as any;
// for (const key in files) {
//   const filename = key.replace(/(\.\/modules\/|\.(js|ts))/g, "");
//   if (!noModulesName.includes(filename)) {
//     const file = files[key].default || files[key];
//     modules.push(file);
//   }
// }
// console.log(modules)

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/login.vue"),
    meta: {
      title: "登录",
      hidden: true,
      icon: "dashboard",
    },
  },
  {
    path: "/",
    name: "Root",
    redirect: "/astar",
    component: Layout,
    meta: {
      isLogin: true,
      hidden: true,
    },
  },
  {
    path: "/ceshi",
    name: "ceshi",
    component: () => import("@/views/ceshi.vue"),
    meta: {
      isLogin: true,
      hidden: true,
    },
  },
  {
    path: "/3dtit",
    name: "3dtit",
    component: () => import("@/views/3dtit.vue"),
    meta: {
      isLogin: true,
      hidden: true,
    },
  },
  {
    path: "/baidu",
    name: "baidu",
    component: () => import("@/views/baidu/index.vue"),
    meta: {
      isLogin: true,
      hidden: true,
    },
  },
  {
    path: "/astar",
    name: "astar",
    component: () => import("@/views/astar/index.vue"),
    meta: {
      isLogin: true,
      hidden: true,
    },
  },
  {
    path: "/astarab",
    name: "astarab",
    component: () => import("@/views/astar/ab.vue"),
    meta: {
      isLogin: true,
      hidden: true,
    },
  },
  {
    path: "/redirect",
    name: "Redirect",
    children: [
      {
        path: "/redirect/:path(.*)",
        name: "Redirect",
        component: () => import("@/views/Redirect/Redirect.vue"),
        meta: {},
      },
    ],
    meta: {
      hidden: true,
      noTagsView: true,
      icon: "dashboard",
    },
  },
  {
    path: "/index",
    name: "index",
    redirect: "/index/home",
    component: LayoutView,
    meta: {
      isLogin: true,
      title: "主页",
      icon: "home",
    },
    children: [
      {
        path: "home",
        component: () => import("@/views/index.vue"),
        name: "home",
        meta: {
          title: "主页",
          affix: true,
          icon: "home",
        },
      },
    ],
  },
  // {
  //   path: "/404",
  //   component: () => import("@/views/error/404.vue"),
  //   name: "NoFind",
  //   meta: {
  //     hidden: true,
  //     title: "404",
  //     icon: "dashboard",
  //     noTagsView: true,
  //   },
  // },
];
export const asyncRouterMap: AppRouteRecordRaw[] = [

  // ...modules,
];

const router = createRouter({
  history: createWebHistory(),
  strict: false,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

export const resetRouter = (): void => {
  const resetWhiteNameList = ["Redirect", "Login", "NoFind", "Root"];
  router.getRoutes().forEach((route) => {
    const { name } = route;
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
};

export default router;
